How to efficiently resize an array of values, without boxing, used within a Dictionary<string, float[]>

Posted by makerofthings7 on Stack Overflow See other posts from Stack Overflow or by makerofthings7
Published on 2011-01-18T02:23:01Z Indexed on 2011/01/18 2:53 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

In the code below, Pages is defined as

 public SortedDictionary<DateTime, float[]> Pages { get; set; }

I am trying to dynamically increase the size of this array. Can anyone tell how to increase the sized of the innermost float[]?

 var tt = currentContainer.Pages[dateTime];
 Array.Resize<float>(ref tt, currentContainer.Pages.Count + 1);

Fail 1

I tried the following code and get index out of range exception

    SortedDictionary<DateTime, float[]> Pages = new SortedDictionary<DateTime,float[]>();
    float[] xx = new float[1];
    xx[0] = 1;
    DateTime tempTime = DateTime.UtcNow;
    Pages.Add(tempTime, xx);
    var tt = Pages[tempTime];
    Array.Resize<float>(ref tt, Pages.Count + 1);
    Pages[tempTime][1] = 2;

Fail 2

The following gives a compile time error (property, index, or dynamic member can't be used as a ref value)

    SortedDictionary<DateTime, float[]> Pages = new SortedDictionary<DateTime,float[]>();
    float[] xx = new float[1];
    xx[0] = 1;
    DateTime tempTime = DateTime.UtcNow;
    Pages.Add(tempTime, xx);
    var tt = Pages[tempTime];
    // The line below is different from Fail 1 above ... compile time error
    Array.Resize<float>(ref Pages[tempTime], Pages.Count + 1);
    Pages[tempTime][1] = 2;

Question

What is the most performant answer to resize this array?

Would the answer change if it's likely that the final size will be 100-200 floats or 700-900 floats?

What if I change my allocation size from +1 to +128? .. or larger?

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays